<template>
  <div
    class="wrap-item"
    :style="{
      '--scroll-width': '175px',
    }"
  >
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model="formSearch.param.orderName"
          maxlength="20"
          placeholder="请输入工单名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formSearch.param.title"
          maxlength="50"
          placeholder="请输入工单标题"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-cascader
          style="width: 100%"
          v-model="formSearch.param.typeId"
          :options="typeData"
          @change="handleChange"
          :props="{
            value: 'id',
            label: 'typeName',
            children: 'childList',
            expandTrigger: 'hover',
            emitPath: false,
            checkStrictly: true,
          }"
          :checkStrictly="true"
          :show-all-levels="false"
          placeholder="请选择工单类型"
          ref="typeCascader"
          clearable
          popper-class="theder"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="formSearch.param.orderType"
          placeholder="请选择工单类别"
          clearable
        >
          <el-option label="流程类" :value="0"></el-option>
          <el-option label="指派类" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.status"
          placeholder="请选择状态"
        >
          <el-option label="待指派" value="0"></el-option>
          <el-option label="待接单" value="1"></el-option>
          <el-option label="处理中" value="2"></el-option>
          <el-option label="已处理" value="3"></el-option>
          <el-option label="待审批" value="4"></el-option>
          <el-option label="审批中" value="5"></el-option>
          <el-option label="已审批" value="6"></el-option>
          <el-option label="已驳回" value="7"></el-option>
          <el-option label="已归档" value="8"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="formSearch.param.date"
          @change="dateChange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始创建时间"
          end-placeholder="结束创建时间"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-other-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'childList', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="orderName" label="工单名称"></el-table-column>
        <el-table-column prop="title" label="工单标题">
          <template slot-scope="scope">
            <el-tooltip
              popper-class="ellipsis-tip"
              :content="scope.row.title"
              placement="top-start"
            >
              <span class="ellipsis">{{ scope.row.title }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="typeName" label="工单类型"></el-table-column>
        <el-table-column prop="workType" label="工单类别">
          <template slot-scope="scope">
            <span v-if="scope.row.workType == 0">流程类</span>
            <span v-if="scope.row.workType == 1">指派类</span>
          </template>
        </el-table-column>
        <el-table-column prop="expireDataObj" label="处理时限">
          <template slot-scope="scope">
            <span v-if="scope.row.expireDataObj.type == 0"
              >{{ scope.row.expireDataObj.expire }}天</span
            >
            <span v-if="scope.row.expireDataObj.type == 1"
              >{{ scope.row.expireDataObj.expire }}小时</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="submitUserName" label="联系人"></el-table-column>
        <el-table-column
          prop="submitUserPhone"
          label="联系电话"
        ></el-table-column>
        <el-table-column prop="appointStatus" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.appointStatus == 0">待指派</span>
            <span v-if="scope.row.appointStatus == 1">待接单</span>
            <span v-if="scope.row.appointStatus == 2">处理中</span>
            <span v-if="scope.row.appointStatus == 3">已处理</span>
            <span v-if="scope.row.appointStatus == 4">待审批</span>
            <span v-if="scope.row.appointStatus == 5">审批中</span>
            <span v-if="scope.row.appointStatus == 6">已审批</span>
            <span v-if="scope.row.appointStatus == 7">已驳回</span>
            <span v-if="scope.row.appointStatus == 8">已归档</span>
          </template>
        </el-table-column>
        <el-table-column prop="nodeName" label="处理人"></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="150px"
        ></el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item
                  command="edit"
                  v-if="$checkPermission(102) && scope.row.appointStatus == 7"
                  >修改</el-dropdown-item
                >
                <el-dropdown-item
                  command="cancellation"
                  v-if="$checkPermission(121) && scope.row.appointStatus == 7"
                  >作废</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 对话框 -->
    <el-drawer
      title="修改工单"
      :wrapperClosable="false"
      :visible.sync="visible"
    >
      <el-form
        :label-width="labelWidth"
        :label-position="labelPosition"
        :model="form"
        :size="size"
      >
        <el-form-item label="工单标题" required>
          <div style="margin: 0 10px">
            <el-input
              v-model.trim="form.title"
              placeholder="请输入工单标题"
              maxlength="50"
              clearable
              style="width: 100%"
            ></el-input>
          </div>
        </el-form-item>
        <handle-form ref="form" :form="form.form" :formItem="form.formItem" />
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false">取消</span>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <detail-item :visible="isLook" :data="lookData" @close="isLook = false" />
  </div>
</template>

<script>
import detailItem from './../components/detail'
export default {
  components: { detailItem },
  data() {
    return {
      typeData: [],
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      visible: false,
      form: {},
      labelWidth: '100px',
      labelPosition: 'left',
      size: 'medium',
      lookData: {},
      isLook: false,
    }
  },
  created() {
    this.typeList()
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios
        .post('/workorder/myToDoAppoint/myToDoLaunch', this.formSearch)
        .then((data) => {
          this.tableData = []
          if (data.code == 200) {
            this.tableData = data.data.list
            this.total = data.data.total
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.$axios
            .get('/workorder/workOrder/workOrderProcessDetail?id=' + row.id)
            .then((data) => {
              this.lookData = {}
              if (data.code == 200) {
                this.lookData = data.data
                this.isLook = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'edit':
          //修改
          this.$axios
            .get('/workorder/workOrder/workOrderQueryDetail?id=' + row.id)
            .then((data) => {
              this.form = {}
              if (data.code == 200) {
                this.form = data.data
                this.form.form = JSON.parse(data.data.form)
                this.labelWidth = this.form.form.labelWidth + 'px'
                this.labelPosition = this.form.form.labelPosition
                this.size = this.form.form.size
                this.form.formItem = JSON.parse(data.data.formItem)
                this.visible = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'cancellation':
          //作废
          this.$confirm('确认作废该条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(() => {
            this.$axios
              .get('/workorder/workOrder/workOrderDetails?id=' + row.id)
              .then((data) => {
                if (data.code == 200) {
                  this.$message.success('作废成功!')
                  this.itemList()
                } else {
                  this.$message.error(data.msg)
                }
              })
          })
          break
      }
    },
    save() {
      //保存
      if (!this.form.title) {
        this.$message.error('请输入工单标题')
        return
      }
      const form = this.$refs.form.save()
      if (!form) return
      this.$axios
        .post('/workorder/workOrder/workOrderLnitiateSave', {
          form: JSON.stringify(this.form.form),
          formItem: JSON.stringify(this.form.formItem),
          nameId: this.form.nameId,
          processContent: JSON.stringify(this.form.processContent),
          title: this.form.title,
          id: this.form.id,
        })
        .then((data) => {
          if (data.code == 200) {
            this.visible = false
            this.$message.success('操作成功')
            this.itemList()
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    dateChange(val) {
      if (val) {
        this.formSearch.param.startTime = val[0]
        this.formSearch.param.endTime = val[1]
      } else {
        this.formSearch.param.startTime = ''
        this.formSearch.param.endTime = ''
      }
    },
    typeList() {
      // 获取工单类型
      this.$axios
        .post('/workorder/configWorkOrderTypeManage/workTypeList')
        .then((data) => {
          this.typeData = []
          if (data.code == 200) {
            this.typeData = data.data
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    handleChange(val) {
      this.$refs.typeCascader.dropDownVisible = false //监听值发生变化就关闭它
    },
  },
}
</script>
